import 'package:video_player/video_player.dart';
import 'package:flutter/material.dart';

class ChewieListItem extends StatefulWidget {
  @override
  final String url;
  final bool autoPlay;

  ChewieListItem({
    this.url,
    this.autoPlay, 
    Key key,
  }) : super(key: key);
  _ChewieListItemState createState() => _ChewieListItemState();
}

class _ChewieListItemState extends State<ChewieListItem> {
  VideoPlayerController _controller;
  bool playshow = false;
  @override
  void initState() {
    super.initState();
    _controller = VideoPlayerController.network(widget.url)
      ..initialize().then((_) {
        setState(() {});
      });
    _controller.setLooping(true);
    if (widget.autoPlay) {
      _controller.play();
    } else {
      _controller.pause();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: GestureDetector(
        onTap: () {
          if (_controller.value.isPlaying) {
            _controller.pause();
            setState(() {
              this.playshow = true;
            });
          } else {
            _controller.play();
            setState(() {
              this.playshow = false;
            });
          }
        },
        child: Center(
          child: Stack(
            children: <Widget>[
              new Align(
                alignment: Alignment.center,
                child: _controller.value.initialized
                  ? AspectRatio(
                      aspectRatio: _controller.value.aspectRatio,
                      child: VideoPlayer(_controller),
                    )
                  : Container(
                      width: 40,
                      height: 40,
                      child: Image.asset(
                        'assets/videoloding.gif',
                        fit: BoxFit.cover,
                      ),
                    ),
              ),
              new Align(
                alignment: Alignment.center,
                child: Container(
                  width: 40,
                  height: 40,
                  child: playshow
                      ? Image.asset(
                          'assets/ic_playing.png',
                          fit: BoxFit.cover,
                        )
                      : Container(),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }
}
